﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Server
{
    /// <summary>
    /// 将Trace信息写入Textbox的调试侦听器类
    /// </summary>
    public class TextBoxTraceListener : TraceListener
    {
        private TextBox textBox;
        private Action<string> invokeWrite;

        public TextBoxTraceListener(TextBox textbox)
        {
            textBox = textbox;
            invokeWrite = SendString;
        }

        public override void Write(string message)
        {
            textBox.Invoke(invokeWrite, new object[] { message });
        }

        public override void WriteLine(string message)
        {
            textBox.Invoke(invokeWrite, new object[]
                { message + Environment.NewLine });
        }

        private void SendString(string message)
        {
            // No need to lock text box as this function will only 
            // ever be executed from the UI thread
            textBox.Text += message;
        }
    }

}
